﻿/* 
 * Since 2008 - 2012
 */

package com.hh.bss.project.dao.impl;

import java.util.*;

import com.hh.bss.base.*;
import com.hh.bss.common.ConstantDic;
import com.hh.bss.common.MapObjectRowMapper;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.project.dao.*;
import com.hh.bss.project.entity.*;
import com.hh.bss.project.service.*;
import com.hh.bss.project.vo.query.*;
import com.hh.bss.util.*;
import com.jhlabs.math.VLNoise;





import static com.hh.bss.util.ObjectUtils.*;

import org.springframework.stereotype.Repository;

@Repository
public class SearchProjectInterviewDaoImpl extends BaseHibernateDao<SearchProjectInterview,java.lang.Integer>  implements SearchProjectInterviewDao<SearchProjectInterview,java.lang.Integer>{

	public Class getEntityClass() {
		return SearchProjectInterview.class;
	}
	public Page findConsultantInterviewPage(SearchProjectInterviewQuery query){
		StringBuffer sql = new StringBuffer();
		//人选姓名，职位，客户职位联系人，面试日期，项目负责人，信息录入人，手机，电邮，客户公司
		sql.append(" select * from ( ");
		//sql.append(" select concat(ifnull(a.ref_cvid,0),ifnull(a.company_id,0)) id,a.project_id projectId,a.company_id companyId,a.call_dt callDt,'' cvSearchStatus,b1.name companyName,a.ref_cvid cvId,b.cv_name cvName,f.mobile,h.phone,c.deptName deptName,d.titleName titleName,e.current_salary currentSalary,i.email,j.tag,b.sex,b.function,b.level,b.degree,b.work_year workYear,b.expect_location expectLocation from (select project_id,company_id,dept_id,title_id,ref_cvid,max(call_dt) call_dt from company_cc  group by company_id,dept_id,title_id,project_id,ref_cvid) a ");
		sql.append(" select a.id,a.job_id jobId,b.cv_name cvName,a.name jobName,d.cv_name jobContacter,a.plan_dt interviewDt,e.account owner,b1.name custName,a.creator,b.mobile,b.tel phone,b.email  from (select distinct a0.id,a0.creator,a0.plan_dt,a0.cv_id,a1.job_id,a2.name,a2.hr_contacter,a2.ar_contacter,a2.cust_id from search_project_interview a0,contract_job_cv_recom a1,contract_job a2 where a0.cv_id=a1.cv_id and a1.job_id=a2.id) a ");
		sql.append(" left join  cv b on a.cv_id=b.id ");
		//customer_contacter
		sql.append(" left join  (select t2.id,t1.name,t1.full_name from company t1,customer t2 where t1.id=t2.company_id ) b1 on a.cust_id=b1.id ");
		sql.append(" left join  (select a0.id,a0.cv_name,a1.role,a1.cust_id from cv a0,customer_contacter a1 where a0.id=a1.cv_id ) d on a.cust_id=d.cust_id and d.role='").append(ConstantDic.CUSTOMER_CONTACTER_TYPE_JOB_CONTACTER).append("' ");
		sql.append(" left join (select  t1.job_id,min(t2.account) account from   contract_job_search_project t1,search_project t2 where t1.project_id=t2.id group by t1.job_id ) e on a.job_id=e.job_id ");
		//sql.append(" left join (select cv_id,group_concat((nbr) separator ',') as mobile from cv_contact_detail where contact_type='").append(ConstantDic.ContactType_MOBILE).append("' group by cv_id) f on a.cv_id=f.cv_id ");
		//sql.append(" left join (select cv_id,group_concat((concat(if(nbr_ext1 is null,'','('),ifnull(nbr_ext1,''),if(nbr_ext1 is null,'',')'),ifnull(nbr,''),if(nbr_ext2 is null,'','-'),ifnull(nbr_ext2,''))) separator ',') as phone from cv_contact_detail where contact_type='").append(ConstantDic.ContactType_TEL).append("' group by cv_id) h on a.cv_id=h.cv_id ");
		//sql.append(" left join (select cv_id,group_concat((nbr) separator ',') as email from cv_contact_detail where contact_type='").append(ConstantDic.ContactType_EMAIL).append("' group by cv_id) i on a.cv_id=i.cv_id ");
		//sql.append(" left join (select cv_id,group_concat((tag) separator ',') as tag from cv_tag group by cv_id) j on a.cv_id=j.cv_id ");
		sql.append(" ) comcv ");
		List values = new ArrayList();
		
		//sql.append(" where comcv.projectId=? ");
		//values.add(query.getProjectId());
		sql.append(" where comcv.jobId=? ");
		values.add(query.getJobId());
//		if(isNotEmpty(query.getCompanyId())) {
// 		    sql.append(" and comcv.companyId=? ");
//		    values.add(query.getCompanyId());
//		}
		
		//有cc
//		if ("1".equals(query.getHasCc())){
//			sql.append(" and exists( select 1 from ( select a.ref_cvid from company_cc a where exists (select 1 from search_project_company b where a.company_id=b.company_id and b.project_id=? )) bb where comcv.cvId=bb.ref_cvid ) ");
//		    values.add(query.getProjectId());
//		}
//		if ("1".equals(query.getHasCvlv())){
//			sql.append(" and exists (select 1 from cv a where ( exists (select 1 from cv_education a1 where a.id=a1.cv_id) or  exists (select 1 from cv_certificate a2 where a.id=a2.cv_id) or  exists (select 1 from cv_trainning a3 where a.id=a3.cv_id) or  exists (select 1 from cv_work a4 where a.id=a4.cv_id)) and comcv.cvId=a.id)  ");		    
//		}

		
		//return super.getJdbcDao().createPage(query, values, mapper, pageMaxSize, pageNumber)
		return super.getJdbcDao().createPage(sql.toString(),values.toArray(), new MapObjectRowMapper(), query.getPageSize(), query.getPageNumber()); 
	}
	
	public Page findPage(SearchProjectInterviewQuery query) {
        //XsqlBuilder syntax,please see http://code.google.com/p/rapid-xsqlbuilder
        // [column]为字符串拼接, {column}为使用占位符. [column]为使用字符串拼接,如username='[username]',偷懒时可以使用字符串拼接 
        // [column] 为PageRequest的属性
        
		
		    Class[] os={SearchProjectInterviewQuery.class};
    		Class property= org.springframework.beans.BeanUtils.findPropertyType(query.getField_type(), os);
        if (property!=null){
        	 if ("java.lang.String".equals(property.getName())){
	    			
	    			com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(),query.getQuery());
	    			
	    		}
	    		else if ("java.lang.Integer".equals(property.getName())){
	    			
	    			com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), StringHelper.parseInteger(query.getQuery(), 0));
	    			
	    		}
	    		else if ("java.lang.Double".equals(property.getName())){
	    			com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), StringHelper.parseDouble(query.getQuery(), 0));
	    		}
	    		else if ("java.util.Date".equals(property.getName())){
	    			com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), DateHelper.toDate(query.getQuery(), DateHelper.DEFAULT_DATE_FORMAT));
	    		}
        }	 
        //生成sql2的原因是为了不喜欢使用xsqlbuilder的同学，请修改生成器模板，删除本段的生成
        StringBuffer sql2 = new StringBuffer();
        
        sql2.append("select * from ( ");
        sql2.append(" select a.id,a.project_id projectId,a.cv_id cvId,b.cv_name cvName,a.interviewer,a.plan_dt planDt,a.plan_minutes planMinutes,a.calendar_id calendarId,a.room_id roomId,a.address,a.content,a.iv_dt ivDt,a.iv_minutes ivMinutes,a.result,a.result_desc resultDesc,a.creator,a.create_dt createDt,a.modifier,a.modified_dt modifiedDt from search_project_interview a,cv b where a.cv_id=b.id ");
        sql2.append(" ) t where 1=1 ");
        
        List values = new ArrayList();
                
        if(isNotEmpty(query.getId())) {
            sql2.append(" and  t.id = ? ");
            values.add(query.getId());
        }
        
//         if(isNotEmpty(query.getProjectId())) {
//            sql2.append(" and  t.projectId = ? ");
//            values.add(query.getProjectId());
//        }
        if(isNotEmpty(query.getCvId())) {
            sql2.append(" and  t.cvId = ? ");
            values.add(query.getCvId());
        }
        if(isNotEmpty(query.getJobId())) {
            sql2.append(" and  exists(select 1 from contract_job_search_project a1,contract_job a2 where a2.id=? and a2.id=a1.job_id and a1.project_id=t.projectId ) ");
            values.add(query.getJobId());
        }
        if(isNotEmpty(query.getInterviewer())){
        	sql2.append(" and t.interviewer = ? ");
        	values.add(query.getInterviewer());
        }
        if(isNotEmpty(query.getAddress())){
        	sql2.append(" and t.address = ? ");
        	values.add(query.getAddress());
        }
        if(isNotEmpty(query.getContent())){
        	sql2.append(" and  t.content like ? ");
            values.add("%"+query.getContent()+"%"); 
        }
        if(isNotEmpty(query.getPlanDtBegin())){
        	sql2.append(" and t.planDt >= ? " );
        	values.add(query.getPlanDtBegin());
        }
        if(isNotEmpty(query.getPlanDtEnd())){
        	sql2.append(" and t.planDt <= ? ");
        	values.add(query.getPlanDtEnd());
        }
        if(isNotEmpty(query.getResultDesc())){
        	sql2.append(" and t.resultDesc like ? ");
        	 values.add("%"+query.getResultDesc()+"%"); 
        	
        }
        return super.getJdbcDao().createPage(sql2.toString(),values.toArray(), new MapObjectRowMapper(), query.getPageSize(), query.getPageNumber());
	}
	

}
